package ejercicios;

import interfaces.Node;
import interfaces.tests.TraverseTreeInWidth;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TraverseTreeInWidthImpl implements TraverseTreeInWidth{
	public List<Node> traverseTreeInWidth(Node root) {
    	List<Node> result = new ArrayList<Node>();
    	List<Node> currentChildren = Arrays.asList(root);
    	List<Node> newChildren;

    	while (!currentChildren.isEmpty()){
    		result.addAll(currentChildren);
    		
    		newChildren = new ArrayList<Node>();
    		for (Node node : currentChildren){
    			newChildren.addAll(node.getChildren());
    		}
    		currentChildren = newChildren;
    	}
    	
    	return result;
    }
}